﻿/*-----------------------------------------------
// File: ModelLoader.cs 
// Description: 针对model对象的Loader 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using AssetLoad;

namespace AssetLoad
{

    public class ModelLoader : LoaderBase
    {
        public override string GetPrefix()
        {
            return @"model/";
        }

        public IEnumerator LoadModelExtend(string resourceName, LoadComplete callback)
        {
            string abName = GetPrefix() + resourceName;
            abName = abName.ToLower();
            AssetBundleManager.Instance.LoadAssetBundle(abName);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(abName))
            {
                yield return null;
            }

            if (callback != null)
            {
                var obj = AssetBundleManager.Instance.GetAssetFromBundle(abName,
                    CommonMethod.GetAssetNameFromAssetBundleName(resourceName),typeof(AnimClipsAsset));
                callback(obj);
            }
        }

        public void UnloadModelExtend(string resourceName)
        {
            string abName = GetPrefix() + resourceName;
            abName = abName.ToLower();
            AssetBundleManager.Instance.UnloadAssetBundle(abName);
        }

        public IEnumerator LoadModelLife(string resourceName, LoadComplete callback)
        {
            string abName = GetPrefix() + resourceName;
            var obj = GameObjectPoolManager.Instance.Spawn(abName, resourceName);
            if(obj == null)
            {
                yield return RegistLifePoolAndSpawnObj(resourceName, callback);
            }
            else
            {
                if (callback != null)
                    callback(obj);
            }
        }
        
        
        

        public IEnumerator RegistLifePoolAndSpawnObj(string resourceName, LoadComplete callback)
        {
            string abName = GetPrefix() + resourceName;
            abName = abName.ToLower();
            AssetBundleManager.Instance.LoadAssetBundle(abName);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(abName))
            {
                yield return null;
            }
            GameObjectPoolManager.Instance.Regist(abName, resourceName);

            if (callback != null)
            {
                var obj = GameObjectPoolManager.Instance.Spawn(abName, resourceName);
                callback(obj);
            }
        }
        public void UnloadModelLife(GameObject obj)
        {
            GameObjectPoolManager.Instance.Unspawn(obj);
        }

        public void UnRegisLifePool(string resourceName)
        { 
            string abName = GetPrefix() + resourceName;
            abName = abName.ToLower();
            GameObjectPoolManager.Instance.UnRegist(abName, resourceName);
            AssetBundleManager.Instance.UnloadAssetBundle(abName);
        }



        public IEnumerator LoadModel(string resourceName, LoadComplete callback)
        {
#if !FIX_RESOURCE
#if UNITY_EDITOR
            System.DateTime dt = System.DateTime.Now;
#endif

            string abName = GetPrefix() + resourceName;
            abName = abName.ToLower();
            AssetBundleManager.Instance.LoadAssetBundle(abName);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(abName))
            {
                yield return null;
            }
            GameObjectPoolManager.Instance.Regist(abName, resourceName);

            if (callback != null)
            {
                var obj = GameObjectPoolManager.Instance.Spawn(abName, resourceName);
                callback(obj);
            }
#if UNITY_EDITOR
            Debug.Log("load model " + resourceName + " cost time: " + (System.DateTime.Now - dt).TotalMilliseconds);
#endif

#else
            string path = GetPrefix() + resourceName;
            ResourceManager.Instance.LoadResource(path);
            GameObjectPoolManager.Instance.Regist(path);
            var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiResByModelName(resourceName);
            foreach (var d in dict) {
                ResourceManager.Instance.LoadResource(@"face/" + d.FaceName);
            }
            yield return 0;
            if (callback != null) {
                var obj = GameObjectPoolManager.Instance.Spawn(path);
                callback(obj);
            }
#endif
        }

        public void UnloadModel(GameObject obj, string resourceName)
        {

#if !FIX_RESOURCE
            GameObjectPoolManager.Instance.Unspawn(obj);
            string abname = GetPrefix() + resourceName;
            abname = abname.ToLower();
            GameObjectPoolManager.Instance.UnRegist(abname, resourceName);
            AssetBundleManager.Instance.UnloadAssetBundle(abname);
#else
            GameObjectPoolManager.Instance.Unspawn(obj);
            string path = GetPrefix() + resourceName;
            GameObjectPoolManager.Instance.UnRegist(path);
            var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiResByModelName(resourceName);
            foreach (var d in dict) {
                ResourceManager.Instance.UnloadResource(@"face/" + d.FaceName);
            }
            ResourceManager.Instance.UnloadResource(path);
#endif
        }

        //public GameObject LoadModel(string resourceName)
        //{
        //    string path = GetPrefix() + resourceName;
        //    ResourceManager.Instance.LoadResource(path);
        //    GameObjectPoolManager.Instance.Regist(path);
        //    var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiResByModelName(resourceName);
        //    foreach (var d in dict) {
        //        ResourceManager.Instance.LoadResource(@"face/" + d.FaceName);
        //    }

        //    GameObject obj = GameObjectPoolManager.Instance.Spawn(path);
        //    obj.name = resourceName;
        //    return obj;
        //}

        //public void UnloadModel(GameObject model, string resourceName)
        //{
        //    GameObjectPoolManager.Instance.Unspawn(model);
        //    string path = GetPrefix() + resourceName;
        //    GameObjectPoolManager.Instance.UnRegist(path);
        //    var dict = Dict.Blo.DictHeroBlo.GetTableHeroEmojiResByModelName(resourceName);
        //    foreach (var d in dict) {
        //        ResourceManager.Instance.UnloadResource(@"face/" + d.FaceName);
        //    }
        //    ResourceManager.Instance.UnloadResource(path);
        //}
        //public GameObject GetModel(string resourceName)
        //{
        //    return GameObjectPoolManager.Instance.Spawn(GetPrefix()+resourceName);
        //}

    }
}